<template>
	<!-- 产品详情 -->
	<view :class="pageClass" :style="pageStyle" v-if="pageReady">
		<view class="index">
			<moxi-nav :set="navSet"></moxi-nav>

			<view class="banner">
				<u-swiper :list="list" @change="changeSwiper($event)" :autoplay="false" :indicator="false"
					@click="viewImg($event, list)" imgMode="aspectFit" :circular="true">
					<!-- <view slot="indicator" class="indicator-num">
						<text
							class="indicator-num__text">{{ list.length == 0 ? currentNum : currentNum + 1 }}/{{ list.length }}</text>
					</view> -->
				</u-swiper>
			</view>

			<view class="msg">
				<view class="price">
					<view class="left" :style="'color:'+app_color">&yen;<view class="money">{{infoData.price}}</view>
					</view>
					<view class="right"><u-icon name="eye" color="#a9a9a9" size="14"></u-icon><text
							style="margin-left: 2px;"> {{infoData.view}}</text></view>
				</view>
				<view class="name">{{infoData.title}}</view>
			</view>

			<view class="affiliation" @click="goEnterprise">
				<view class="left" v-if="infoData.qiye">
					<img style="width: 50px;height: 50px;object-fit: cover;border-radius: 50%;box-shadow: 0 0 3px #bfbfbf;"
						model="aspectFill" :src="infoData.qiye.logo" alt="">
					<view class="name" style="">{{infoData.qiye.name}}</view>
				</view>
				<view class="right">进入主页<u-icon name="arrow-right" size="18"></u-icon></view>
			</view>
<view class="affiliation" style="padding: 0;" v-if="ad.cpc_img&&ad.cpc_img.length">
	<image :src="ad.cpc_img" 
				 mode="widthFix" style="width: 100%;" @click="navto(ad.cpc_url)"></image>
</view>
			<view class="introduce">
				<u-divider text="产品详情" textSize="14" ></u-divider>
				<u-parse :content="infoData.content"></u-parse>
			</view>

			<view class="footer">
				<view class="left">
					<u-tabbar :fixed="false" :placeholder="false" :safeAreaInsetBottom="false">
						<u-tabbar-item text="首页" icon="home" @click="chooseFooter"></u-tabbar-item>
						<u-tabbar-item text="返回" icon="account" @click="goBack()"></u-tabbar-item>
						<u-tabbar-item text="收藏夹" icon="bookmark" @click="chooseFooter"></u-tabbar-item>
					</u-tabbar>
				</view>
				<view class="right">
					<view class="btns">
						<view class="btn" @click="add_collect()" v-if="infoData.collect == false">收藏</view>
						<view class="btn" @click="add_collect()" v-else>已收藏</view>
						<view class="btn" @click="tel">联系商家</view>
					</view>
				</view>
			</view>
			<view style="width: 100%;height: 51px;"></view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				currentNum: 0,
				types: ['产品详情', '产品类型'],
				current: 0,
				phone: '',
				isFavorites: false,
				info: {
					id: "",
					page: 1,
					page_num: 10,
					la: "",
					lg: "",
					search: "",
					province: "",
					city: "",
					c_time: "",
					type: "",
					tag: "",
				},
				infoData: {},
				qiyeId: '',
				ad:{}
			}
		},
		onLoad() {
			if (this.param.id) {
				this.info.id = this.param.id
				this.get()
			}
		},
		methods: {
			goBack() {
				if (getCurrentPages().length > 1) {
					uni.navigateBack({
						delta: 1,
					})
					// this.navto()
				} else {
					this.navto('pages/index/index')
				}
			},
			viewImg(e, data) {
				if (data[e].type == 'video') {
					return
				}
				let currentIndex = ''
				let arr = []
				if (data[0].type == 'video') {
					currentIndex = e - 1
					let a = data.slice(1)
					a.forEach(item => {
						arr.push(item.url)
					})
				} else {
					data.forEach(item => {
						arr.push(item.url)
					})
					currentIndex = e
				}
				uni.getSystemInfo({
					success: (res) => {
						uni.previewImage({
							urls: arr,
							current: currentIndex,
							loop: true,
							success() {
							},
							fail(err) {
								console.error('图片预览失败', err);
							}
						})
					}
				})


			},
			changeSwiper(e) {
				this.currentNum = e.current
			},
			// 获取数据
			get() {
				let self = this
				this.moxiPost('/plugin/zx/index/info/get_product', this.info, function(data) {
					self.infoData = data.data;
					if(data.ad){
						self.ad = data.ad;
						self.$forceUpdate()
					}
					let list = self.infoData.img_list.map(item => ({
						url: item,
						type: 'image'
					}))
					if (data.data.video) {
						list.unshift({
							url: data.data.video,
							poster: data.data.video_img,
							type: 'video'
						})
					}
					self.list = list
					self.qiyeId = self.infoData.qid
				})
			},
			// 加入收藏夹
			add_collect() {
				let self = this;
				this.moxiPost('/plugin/zx/user/product/add_collect', {id:this.infoData.id}, function(data) {
					if(self.infoData.collect){
						self.infoData.collect = false;
					}else{
						self.infoData.collect = true;
					}
					self.msg(data.hint)
				})
				
			},
			
			tel() {
				let str = 'tel?tel=' + this.infoData.qiye.tel;
				this.navto(str)
			},
			goEnterprise() {
				this.navto('pages/plugin/zx/industry?id=' + this.qiyeId)
			},
			chooseFooter(e) {
				if (e == 0) {
					// 回到首页
					this.navto('pages/index/index')
				} else if (e == 1) {
					// 查看企业
					this.goEnterprise()
				} else {
					// 查看收藏夹
					this.navto('pages/plugin/zx/favorites')
				}
			},
			sectionChange(index) {
				this.current = index;
			}
		}
	}
</script>

<style lang="scss" scoped>
	::v-deep .u-swiper {
		height: 375px !important;
	}

	::v-deep .u-swiper__wrapper,
	::v-deep .u-swiper__wrapper__item__wrapper__image,
	::v-deep .u-swiper__wrapper__item__wrapper__video {
		height: 100% !important;
	}

	.footer {
		width: 100%;
		display: flex;
		justify-content: space-between;
		// align-items: center;
		position: fixed;
		bottom: 0;
		left: 0;
		background-color: #fff;

		.left {
			width: 48%;
		}

		.right {
			width: 52%;
			border-top: .5px solid #dadbde;

			box-sizing: border-box;

			.btns {
				height: 41px;
				display: flex;
				justify-content: center;
				align-items: center;
				padding: 5px 0;
				font-size: 14px;

				.btn {
					display: flex;
					width: 85px;
					justify-content: center;
					align-items: center;
					padding: 0 4px;
					// text-align: center;

					&:nth-of-type(1) {
						height: 100%;
						border-radius: 20px 0 0 20px;
						background-color: #424347;
						color: #fff;
					}

					&:nth-of-type(2) {
						height: 100%;
						border-radius: 0 20px 20px 0;
						background-color: #fe4237;
						color: #fff;
					}
				}
			}
		}
	}

	::v-deep .u-tabbar-item {
		flex: none;
	}

	::v-deep .u-tabbar-item__icon {
		width: 60px !important;
	}

	::v-deep .u-divider {
		margin: 8px 0 !important;
	}

	.introduce {
		width: 94%;
		margin: 10px auto;
		padding: 2px 10px;
		border-radius: 6px;
		box-sizing: border-box;
		background-color: #fff;
	}

	.affiliation {
		width: 94%;
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin: 10px auto;
		padding: 10px;
		border-radius: 6px;
		box-sizing: border-box;
		background-color: #fff;

		.left,
		.right {
			display: flex;
			align-items: center;
		}

		.left {
			width: 80%;

			.name {
				// width: calc(100% - 50px - 10px - 10px - 82px);
				// font-size: 14px;
				padding: 0 6px;
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis
			}
		}

		.right {
			width: 20%;
			align-items: baseline;
			font-size: 14px;
			white-space: nowrap;
		}
	}

	.msg {
		width: 94%;
		margin: 10px auto;
		padding: 10px;
		border-radius: 6px;
		box-sizing: border-box;
		background-color: #fff;

		.price {
			display: flex;
			justify-content: space-between;
			align-items: baseline;

			.left {
				display: flex;
				align-items: baseline;
				font-size: 14px;
				color: #f6453a;

				.money {
					margin-left: 2px;
					font-size: 18px;
					font-weight: bold;
				}
			}

			.right {
				display: flex;
				align-items: baseline;
				font-size: 14px;
				color: #a9a9a9;
			}
		}

		.name {
			// height: 48px;
			font-size: 18px;
			font-weight: bold;
			overflow: hidden;
			text-overflow: ellipsis;
			display: -webkit-box;
			-webkit-line-clamp: 2;
			-webkit-box-orient: vertical;
		}
	}

	.indicator {
		@include flex(row);
		justify-content: center;

		&__dot {
			height: 6px;
			width: 6px;
			border-radius: 100px;
			background-color: rgba(255, 255, 255, 0.35);
			margin: 0 5px;
			transition: background-color 0.3s;

			&--active {
				background-color: #ffffff;
			}
		}
	}

	.indicator-num {
		padding: 2px 0;
		background-color: rgba(0, 0, 0, 0.35);
		border-radius: 100px;
		width: 35px;
		@include flex;
		justify-content: center;

		&__text {
			color: #FFFFFF;
			font-size: 12px;
		}
	}

	::v-deep .uni-video-container {
		position: relative !important;
	}

	video {
		object-fit: cover;
	}

	.index {
		width: 100%;
		height: 100vh;
		overflow: auto;
		background-color: #f3f4f7;
	}

	* {
		touch-action: none;
	}
</style>